package com.serkol.driving.learning.api;

import com.serkol.bean.MyPage;
import com.serkol.bean.Result;
import com.serkol.constant.ResultConstant;
import com.serkol.driving.learning.entity.Language;
import com.serkol.driving.learning.service.LanguageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("api/language")
@RestController
public class LanguageApiController {
    @Autowired
    private LanguageService service;

    @GetMapping("/findByList")
    public Result findByList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "page", defaultValue = "0") int page) {
        Result result = new Result();
        MyPage list = service.findByList(pageSize, page);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody Language data) {
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {
            flag = service.updateById(data);
        } else {
            flag = service.save(data);
        }
        if (flag) {
            return Result.ok();
        } else {
            return Result.failure();
        }

    }
}
